//-----------------------------------------------------------------------
// <copyright file="CompanyAndMajorDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CompanyAndMajorDAOBase
    {
        #region Common methods
        public virtual CompanyAndMajor CreateCompanyAndMajorFromReader(IDataReader reader)
        {
            CompanyAndMajor item = new CompanyAndMajor();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyAndMajorID"))) item.CompanyAndMajorID = (int)reader["CompanyAndMajorID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyID"))) item.CompanyID = (long)reader["CompanyID"];
                if (!reader.IsDBNull(reader.GetOrdinal("MajorID"))) item.MajorID = (int)reader["MajorID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndMajorFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCompanyAndMajor methods
            
        public virtual void CreateCompanyAndMajor(CompanyAndMajor companyAndMajor)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndMajorInsert");
                
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndMajor.CompanyID);
                database.AddInParameter(dbCommand, "@MajorID", DbType.Int32, companyAndMajor.MajorID);
                database.AddOutParameter(dbCommand, "@CompanyAndMajorID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                companyAndMajor.CompanyAndMajorID = (int)database.GetParameterValue(dbCommand, "@CompanyAndMajorID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndMajorException, ex);
            }
        }

        #endregion

        #region UpdateCompanyAndMajor methods
        
        public virtual void UpdateCompanyAndMajor(CompanyAndMajor companyAndMajor)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndMajorUpdate");            
                
                database.AddInParameter(dbCommand, "@CompanyAndMajorID", DbType.Int32, companyAndMajor.CompanyAndMajorID);
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndMajor.CompanyID);
                database.AddInParameter(dbCommand, "@MajorID", DbType.Int32, companyAndMajor.MajorID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCompanyAndMajorException, ex);
            }
        }
        
        #endregion

        #region DeleteCompanyAndMajor methods
        public virtual void DeleteCompanyAndMajor(int companyAndMajorID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndMajorDelete");
                
                database.AddInParameter(dbCommand, "@CompanyAndMajorID", DbType.Int32, companyAndMajorID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCompanyAndMajorException, ex);
            }
        }

        #endregion

        #region GetCompanyAndMajor methods
        
        public virtual CompanyAndMajor GetCompanyAndMajor(int companyAndMajorID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndMajorGet");
                
                database.AddInParameter(dbCommand, "@CompanyAndMajorID", DbType.Int32, companyAndMajorID);
                
                CompanyAndMajor companyAndMajor =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        companyAndMajor = CreateCompanyAndMajorFromReader(reader);
                        reader.Close();
                    }
                }
                return companyAndMajor;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndMajorException, ex);
            }
        }
        
        #endregion

        #region GetCompanyAndMajorList methods
        public virtual CompanyAndMajorCollection GetCompanyAndMajorList(CompanyAndMajorColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndMajorGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndMajorCollection companyAndMajorCollection = new CompanyAndMajorCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndMajor companyAndMajor = CreateCompanyAndMajorFromReader(reader);
                        companyAndMajorCollection.Add(companyAndMajor);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndMajorCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndMajorListException, ex);
            }
        }
        
        public virtual CompanyAndMajorCollection GetCompanyAndMajorList(CompanyAndMajorColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCompanyAndMajorList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCompanyAndMajorList methods
        public virtual CompanyAndMajorCollection GetCompanyAndMajorListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndMajorGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndMajorCollection companyAndMajorCollection = new CompanyAndMajorCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndMajor companyAndMajor = CreateCompanyAndMajorFromReader(reader);
                        companyAndMajorCollection.Add(companyAndMajor);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndMajorCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndMajorListException, ex);
            }
        }
        
        public virtual CompanyAndMajorCollection GetCompanyAndMajorListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCompanyAndMajorListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}